<template>
  <view-box ref="viewBox" body-padding-bottom="5px" style="background: #F5F5F5;">
    <tab style="background: #fff;">
      <tab-item selected @on-item-click="onItemClick('usefulList')">可用优惠券</tab-item>
      <tab-item @on-item-click="onItemClick('unusefulList')">不可用优惠券</tab-item>
    </tab>
    <!-- coupon -->
    <div class="container">
      <empty tips="暂无优惠券" icon="icon-daipingjia" v-if="list !== null && showList.length === 0"></empty>
      <div @click="refer(item)" v-for="(item, index) in showList" :key="index" class="card-detail clearfix" :class="{disabled: show === 'unusefulList'}" v-if="list !== null && showList.length > 0">
        <!-- 左边大色块 -->
        <div class="card-detail-left">
            <div class="card-detail-left-top bg2 bd2">
                <!-- 左边半圆 -->
                <div class="left-circle"></div>
            </div>
            <div class="card-detail-left-bottom bd2"></div>
        </div>
        <!-- 右边大色块 -->
        <div class="card-detail-right">
            <div class="card-detail-right-top bg2">
                <div>全场通用</div>
                <div>&yen;<span>{{item.money}}</span></div>
                <div class="c1">黔韵</div>
                <div class="c1">优惠红包</div>
            </div>
            <div class="card-detail-right-bottom">
                <span>使用期限：{{item.start_use_at.substr(0, 10)}} 至 {{item.end_use_at.substr(0, 10)}}</span>
                <span>
                  <template v-if="item.used_at">已使用</template>
                  <template v-else-if="!largerThanCurrentDate(item.end_use_at)">已过期</template>
                  <template v-else-if="largerThanCurrentDate(item.start_use_at)">不可用</template>
                  <template v-else>未使用</template>
                </span>
            </div>
        </div>
      </div>
    </div>
  </view-box>
</template>
<script>
  import { ViewBox, Tab, TabItem } from 'vux'
  import Empty from '@/views/components/Empty'
  import { SET_CHECKED_COUPON } from '@/store/mutation-types'
  export default {
    data () {
      return {
        list: null,
        show: 'usefulList'
      }
    },
    components: {
      ViewBox,
      Tab,
      TabItem,
      Empty
    },
    computed: {
      usefulList () {
        let list = []
        if (this.list !== null) {
          this.list.forEach((item) => {
            if (item.used_at === null && !this.largerThanCurrentDate(item.start_use_at) && this.largerThanCurrentDate(item.end_use_at)) {
              list.push(item)
            }
          })
        }
        return list
      },
      unusefulList () {
        let list = []
        if (this.list !== null) {
          this.list.forEach((item) => {
            if (item.used_at !== null || this.largerThanCurrentDate(item.start_use_at) || !this.largerThanCurrentDate(item.end_use_at)) {
              list.push(item)
            }
          })
        }
        return list
      },
      showList () {
        if (this.show === 'usefulList') {
          return this.usefulList
        } else {
          return this.unusefulList
        }
      }
    },
    mounted () {
      this._getData()
    },
    methods: {
      _getData () {
        this.$vux.loading.show()
        this.$http.get('/user_coupons').then((response) => {
          this.$vux.loading.hide()
          this.list = response.data
        }).catch((error) => {
          this.$vux.loading.hide()
          console.log(error)
        })
      },
      onItemClick (show) {
        this.show = show
      },
      refer (item) {
        if (this.$route.query.refer && this.show === 'usefulList') {
          this.$store.commit(SET_CHECKED_COUPON, item)
          this.$router.push(this.$route.query.refer)
        }
      },
      largerThanCurrentDate (date) {
        let d = date.replace(/-/g, '/')
        return new Date() < new Date(d)
      }
    }
  }
</script>
<style scoped lang="less">
  *{box-sizing: border-box;}
  .container{
      margin: 10px auto;
      width: 100%;
      padding: 0 10px;
      max-width: 640px;
  }
  .card-detail{
      position: relative;
      margin-top: 10px;
      width: 100%;
      display: -webkit-box;
      display: -webkit-flex;
      display: -ms-flexbox;
      display: flex;
  }
  .card-detail:first-child{
      margin-top: 0;
  }
  .card-detail-left,
  .card-detail-right{
      position: relative;
      float: left;
  }
  .card-detail-left{
      width: 25%;
  }
  .bd1{
      border-right: 1px dashed #BE8307;
  }
  .bd2{
      border-right: 1px dashed #75A0BD;
  }
  .bd3{
      border-right: 1px dashed #A5A5A5;
  }
  .card-detail-left-top{
      width: 100%;
      height: 120px;
      border-radius: 8px 0 0 0;
  }
  .card-detail-left-top.bg1{
      background-color: #FEAC00;
  }
  .card-detail-left-top.bg2{
      background-color: #1186D6;
  }
  .card-detail-left-top.bg3{
      background-color: #7F7F7F;
  }
  .card-detail-left-top:before,
  .card-detail-left-top:after{
      content: "";
      position: absolute;
      right: -8px;
      display: block;
      width: 16px;
      height: 16px;
      background-color: #F5F5F5;
      border-radius: 50%;
      z-index: 11;
  }
  .card-detail-left-top:after{
      top: -10px;
  }
  .card-detail-left-top:before{
      bottom: -10px;
  }
  .card-detail-left-bottom{
      width: 100%;
      height: 40px;
      background-color: #fff;
      border-radius: 0 0 0 8px;
  }
  .left-circle{
      position: absolute;
      top: 50%;
      left: -25px;
      margin-top: -25px;
      width: 50px;
      height: 50px;
      background-color: #F5F5F5;
      border-radius: 50%;
  }
  .card-detail-right{
      position: relative;
      overflow: hidden;
      -webkit-box-flex:1;
      -webkit-flex:1;
      -ms-flex:1;
      flex:1;
  }
  .overdue{
      position: absolute;
      right: 5px;
      bottom: 0;
      width: 140px;
      z-index: 11;
  }
  .card-detail-right-top{
      position: relative;
      height: 120px;
      border-radius: 0 8px 0 0;
  }
  .card-detail-right-top.bg1{
      background-color: #FFC13E;
  }
  .card-detail-right-top.bg2{
      background-color: #6AB7EC;
  }
  .card-detail-right-top.bg3{
      background-color: #9E9E9E;
  }
  .card-detail-right-top div:nth-child(1){
      position: absolute;
      top: 20px;
      right: 20px;
      color: rgba(255, 255, 255, .8);
      font-size: 16px;
  }
  .card-detail-right-top div:nth-child(2){
      position: absolute;
      top: 35px;
      right: 100px;
      color: #fff;
      font-size: 25px;
  }
  .card-detail-right-top div:nth-child(2) span{
      margin-left: 5px;
      font-size: 60px;
  }
  .card-detail-right-top div:nth-child(3){
      position: absolute;
      top: 40px;
      right: 25px;
      color: #B87D01;
      font-size: 30px;
  }
  .card-detail-right-top div:nth-child(3).c1,
  .card-detail-right-top div:nth-child(4).c1{
      color: #2671A5;
  }
  .card-detail-right-top div:nth-child(3).c2,
  .card-detail-right-top div:nth-child(4).c2{
      color: #5C5C5C;
  }
  .card-detail-right-top div:nth-child(4){
      position: absolute;
      margin-top: 5px;
      top: 70px;
      right: 20px;
      color: #B87D01;
      font-size: 16px;
  }
  .card-detail-right-bottom{
      width: 100%;
      padding-left: 8px;
      padding-right: 8px;
      height: 40px;
      font-size: 10px;
      background-color: #fff;
      border-radius: 0 0 8px 0;
      display: -webkit-box;
      display: -webkit-flex;
      display: -ms-flexbox;
      display: flex;
      -webkit-box-align: center;
      -webkit-align-items: center;
      -ms-flex-align: center;
      align-items: center;
      -webkit-box-pack: justify;
      -webkit-justify-content: space-between;
      -ms-flex-pack: justify;
      justify-content: space-between;
  }
  .card-detail-right-bottom span:nth-child(2){
    text-align: right;
  }
  .disabled .bd2 {
    border-right: 1px dashed #A5A5A5;
  }
  .disabled .card-detail-left-top.bg2{
    background-color: #7F7F7F;
  }
  .disabled .card-detail-right-top.bg2{
    background-color: #9E9E9E;
  }
  .disabled .card-detail-right-top div:nth-child(3), .disabled .card-detail-right-top div:nth-child(4){
      color: #5C5C5C;
  }
  
</style>